package com.wanlian.router.ui.me;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.smart.android.ui.tools.BaseListFragment;
import com.wanlian.router.R;
import com.wanlian.router.bean.NetDevice;
import com.wanlian.router.net.NetWorkingClient;
import com.wanlian.router.net.ResponseData;
import com.wanlian.router.net.internal.INetCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Hyu on 2018/9/8.
 * Email: fvaryu@163.com
 */
public class MyDeviceFragment extends BaseListFragment {

    private List<NetDevice> mNetDevices;
    private MyDeviceAdapter mMyDeviceAdapter;

    public static MyDeviceFragment newInstance() {

        Bundle args = new Bundle();

        MyDeviceFragment fragment = new MyDeviceFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected void initUI(View view) {
        super.initUI(view);
        mNetDevices = new ArrayList<>();
        setAdapter(mMyDeviceAdapter = new MyDeviceAdapter(getActivity(), mNetDevices));
    }

    @Override
    protected void initData(View view) {
        super.initData(view);
        mMyDeviceAdapter.setOnSwitchButtonClickListener(new MyDeviceAdapter.OnSwitchButtonClickListener() {
            @Override
            public void onSwitchButtonClick(NetDevice device) {
                NetWorkingClient.getInstance().switchRouter(getActivity(), device.getDeviceId(), new INetCallBack() {
                    @Override
                    public void onFinish(ResponseData response, @Nullable Object o) {
                        if (response.isSuccess()) {
                            showToast("切换成功");
                            autoRefresh();
                        }
                    }
                });
            }
        });

        mMyDeviceAdapter.setOnDeleteButtonClickListener(new MyDeviceAdapter.OnDeleteButtonClickListener() {
            @Override
            public void onDeleteButtonClick(NetDevice device) {
                NetWorkingClient.getInstance().deleteRouter(getActivity(), device.getDeviceId(), new INetCallBack() {
                    @Override
                    public void onFinish(ResponseData response, @Nullable Object o) {
                        if (response.isSuccess()) {

                            showToast("删除成功");
                            autoRefresh();
                        }
                    }
                });
            }
        });
    }

    @Override
    protected void loadData(final boolean isRefresh) {
        super.loadData(isRefresh);
        if (!canToLoad(isRefresh)) {
            return;
        }

        NetWorkingClient.getInstance().queryUserDevice(getActivity(), getPageInfo(), new INetCallBack<List<NetDevice>>() {
            @Override
            public void onFinish(ResponseData response, @Nullable List<NetDevice> netDevices) {

                resetPull();

                if (response.isSuccess()) {
                    if (isRefresh) {
                        mNetDevices.clear();
                    }
                    if (netDevices != null) {
                        mNetDevices.addAll(netDevices);
                    }

                    mMyDeviceAdapter.notifyDataSetChanged();
                }

            }
        });
    }
}